export default {
    data() {
        return {
            // 父作用域
            mixinEdit_parent:null,
            // 编辑框 显隐
            mixinEdit_editVisible: false,
            // 编辑框 标题
            mixinEdit_editTitle: "",
            // 编辑框 类型
            mixinEdit_editType: "",
            // 缓存编辑模板数据
            mixinEdit_editCacheData: null,

            // 加载状态
            mixinEdit_loading:true,
            // 编辑时选择的数据
            mixinEdit_editSelectedData: null,
        }
    },
    methods: {

        /**
         * form 提交验证
         * @param {*} params_.model 数据模型
         * @param {*} params_.id 修改时的id
         * @param {*} params_.title 窗口标题，可为空
         */
        // 显示编辑窗口
        async mixinEdit_editBoxInit(params_) {
            let _data = null;
             // 显示窗口
             this.mixinEdit_editVisible = true;
            this.mixinEdit_loading = true;
            // 添加
            if (this.mixinEdit_editType == "add") {
                this.mixinEdit_editTitle = "添加";
                _data = this.mixinEdit_editDataExtend({
                    ...this[params_.model]
                });
            }
            // 修改 
            else if (this.mixinEdit_editType == "update") {
                this.mixinEdit_editTitle = "修改";

                let _d = {
                    // delayTime: 1000,
                    data: {
                        id: params_.id
                    },
                }
                if(typeof this.apiUrl.detail=="object"){
                    _d.url = this.apiUrl.detail.url;
                    _d.domain = this.apiUrl.detail.domain;
                }
                else{
                    _d.url = this.apiUrl.detail;
                }
                let _detail = await this.$vFN.Http.post(_d,this.appWindow);
                if (_detail._ok) {
                    // 修改时的数据
                    _data = this.mixinEdit_editDataExtend({
                        ..._detail.data
                    });
                    this.mixinEdit_editSelectedData = _data;
                }
            }

            // 复制 
            else if (this.mixinEdit_editType == "copy") {
                this.mixinEdit_editTitle = "复制";

                let _d = {
                    // delayTime: 1000,
                    data: {
                        id: params_.id
                    },
                }

                if(typeof this.apiUrl.detail=="object"){
                    _d.url = this.apiUrl.detail.url;
                    _d.domain = this.apiUrl.detail.domain;
                }
                else{
                    _d.url = this.apiUrl.detail;
                }

                let _detail = await this.$vFN.Http.post(_d,this.appWindow);
                if (_detail._ok) {
                    // 修改时的数据
                    _data = this.mixinEdit_editDataExtend({
                        ..._detail.data
                    });
                    this.mixinEdit_editSelectedData = _data;
                }
            }
            // 初始化外部依赖
            if(params_.onInit){
                let _init = await params_.onInit();
            }
            

            // 自定义标题
            if (params_.title) {
                this.mixinEdit_editTitle = params_.title;
            }
            let _form = this[params_.model];
            this.mixinEdit_editCacheData = {
                ..._form
            };
            for (let i in _form) {
                // console.error(i,typeof (_form[i]))
                if (typeof (_form[i]) == "number") {
                    _form[i] = _data[i] - 0;
                } else if (typeof (_form[i]) == "string") {
                    _form[i] = _data[i] + "";
                } else {
                    _form[i] = _data[i];
                }
            }

            this.mixinEdit_loading = false;
        },
        // 编辑修改的参数扩展方法
        mixinEdit_editDataExtend(data_) {
            // 添加
            if (this.mixinEdit_editType == "add") {}
            // 修改 
            else if (this.mixinEdit_editType == "update") {}
            return data_
        },

        // 隐藏编辑窗口
        mixinEdit_editBoxClose(ref_) {
            this.mixinEdit_editVisible = false;

            setTimeout(() => {
                this.mixinEdit_resetForm(ref_);
            }, 500)
        },
        /**
         * form 提交验证
         * @param {*} name_ ref名称
         */
        mixinEdit_formValidate(name_) {
            return new Promise((resolve, reject) => {
                this.$refs[name_].validate((valid) => {
                    
                    if (valid) {
                        resolve(true);
                    } else {
                        resolve(false);
                    }
                });
            });
        },
        /**
         * form 提交添加/修改
         * @param {*} model 数据对象
         * @param {*} ref ref名称
         */
        async mixinEdit_editFormSubmit(params_) {
            this.mixinEdit_loading = true;
            let _isValidate = await this.mixinEdit_formValidate(params_.ref);
            if (!_isValidate) {
                this.mixinEdit_loading = false;
                return;
            }

            let _form = this.mixinEdit_editFormSubmitExtend(JSON.parse(JSON.stringify(this[params_.model])));
            // 如果返回false 则退出提交 
            if(!_form){
                this.mixinEdit_loading = false;
                return;
            }

            
            if (this.mixinEdit_editType == "add") {

                let _d = {
                    data: _form,
                }
                if(typeof this.apiUrl.create=="object"){
                    _d.url = this.apiUrl.create.url;
                    _d.domain = this.apiUrl.create.domain;
                }
                else{
                    _d.url = this.apiUrl.create;
                }
                let _data = await this.$vFN.Http.post(_d,this.appWindow);
                if (_data._ok) {
                    this.mixinEdit_editBoxClose(params_.ref);
                    this.onSubmitSuccess({type:this.mixinEdit_editType,resData:_data,params:params_});
                    this.$vFN.Tools.messageSuccess("添加成功");
                }

            } else if (this.mixinEdit_editType == "update") {
                _form.id = this.mixinEdit_editSelectedData.id;
                let _d = {
                    data: _form,
                }
                if(typeof this.apiUrl.update=="object"){
                    _d.url = this.apiUrl.update.url;
                    _d.domain = this.apiUrl.update.domain;
                }
                else{
                    _d.url = this.apiUrl.update;
                }
                let _data = await this.$vFN.Http.post(_d,this.appWindow);
                if (_data._ok) {
                    this.mixinEdit_editBoxClose(params_.ref);
                    this.onSubmitSuccess({type:this.mixinEdit_editType,resData:_data,params:params_});
                    this.$vFN.Tools.messageSuccess("修改成功");
                }
            }

            else if (this.mixinEdit_editType == "copy") {
                let _d = {
                    data: _form,
                }
                if(typeof this.apiUrl.create=="object"){
                    _d.url = this.apiUrl.create.url;
                    _d.domain = this.apiUrl.create.domain;
                }
                else{
                    _d.url = this.apiUrl.create;
                }
                let _data = await this.$vFN.Http.post(_d,this.appWindow);
                if (_data._ok) {
                    this.mixinEdit_editBoxClose(params_.ref);
                    this.onSubmitSuccess({type:this.mixinEdit_editType,resData:_data,params:params_});
                    this.$vFN.Tools.messageSuccess("复制成功");
                }
            }
            this.mixinEdit_loading = false;
        },


        onSubmitSuccess(r){
            
            console.error(r);
            
            console.error(this.mixinEdit_parent)
            // 与列表互动
            if(r.type=='add'||r.type=='copy'){
                
                if(this.mixinEdit_parent.mixinTable_searchData){
                    this.mixinEdit_parent.mixinTable_getTable();
                }
                if(this.mixinEdit_parent.mixinList_getTable){
                    this.mixinEdit_parent.mixinList_getTable(1);
                }
            }
            else{
                if(this.mixinEdit_parent.mixinTable_getTable){
                    this.mixinEdit_parent.mixinTable_getTable();
                }
                if(this.mixinEdit_parent.mixinList_getTable){
                    this.mixinEdit_parent.mixinList_getTable(this.mixinEdit_parent.mixinList_pageData.pageNum);
                }
            }
        },
        // form 提交添加/修改 的参数扩展方法 返回false 则不提交用于扩展验证
        mixinEdit_editFormSubmitExtend(data_) {
            // 添加
            if (this.mixinEdit_editType == "add") {}
            // 修改 
            else if (this.mixinEdit_editType == "update") {}

            return data_
        },

        /**
         * 重置form 表单数据
         * @param {*} name_ 表单的ref
         */
        mixinEdit_resetForm(name_) {
            this.mixinEdit_resetFormExtend(name_);
            this.$refs[name_].resetFields();
        },
        // 清空扩展
        mixinEdit_resetFormExtend() {},


    },
    mounted() {
        let that = this;
        this.$nextTick(() => {});

    }
}